# Example Quota Share Loss Corridor:

# "In the case that the combined ratio (the reinsurer's
# combined ratio is the sum of commission and reinsured
# losses, divided by the reinsurance premium), is 110% or
# more, the reinsured shall be liable for all further losses
# falling under this treaty until a combined ratio of 180% is
# reached."

library(tidyverse)

# We start with a single column with the Gross Claims Ratio - or Gross Loss Ratio 
# (value of claims / premiums earned).
data <- tibble(gross.claims.ratio = seq(from = 0, to = 2.5, by = 0.1))
data

# Add quota share
data <- data %>% mutate(qs.retention = 0.5 * gross.claims.ratio)
data

# Add loss corridor and real retention
data <- data %>% 
  mutate(
    aux.xs.ratio = if_else(gross.claims.ratio < 1.1, 0, gross.claims.ratio - 1.1),
    lc.added = 0.5 * if_else(aux.xs.ratio <= 0.7, aux.xs.ratio, 0.7),
    real.retention = qs.retention + lc.added
  )
data


############## More general way to make the plot ###########

# Prepare data for plot
plot.data <- data %>% 
  # Include scale for the x axis (needed for plot)
  mutate(x = gross.claims.ratio) %>% 
  # Remove auxiliary values
  select(-aux.xs.ratio) %>% 
  pivot_longer(cols = gross.claims.ratio:real.retention)
plot.data

# Static plot - quota share loss corridor
p <- plot.data %>% 
  ggplot(aes(x = x, y = value, color = name)) + 
  geom_line() +
  geom_point() +
  # show x axis scale in %
  scale_x_continuous(labels = scales::percent) +
  # show y axis scale in %
  scale_y_continuous(labels = scales::percent) +
  labs(
    title = "Quota share 50% with Loss Corridor 110%-180%", 
    x = "", y = ""
    ) +
  # Just decorating the plot, not mandatory
  theme_bw() +
  theme(legend.position = "bottom", legend.title = element_blank())
p

# Animated plot
library(plotly)
ggplotly(p)
